/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pos.ui.beans;

import com.pos.domain.PurchaseDetails;
import com.pos.domain.PurchaseLineItem;
import com.pos.ui.jsf.util.JsfUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.enterprise.context.SessionScoped;
import javax.faces.model.SelectItem;
import javax.inject.Named;

/**
 *
 * @author suraj
 */
@Named("purchaseBean")
@SessionScoped
public class PurchaseBean extends TradingBean implements Serializable {

    private Set<PurchaseLineItem> lineItems = new LinkedHashSet<PurchaseLineItem>();

    public List<PurchaseLineItem> getLineItems() {
        return new ArrayList<PurchaseLineItem>(lineItems);
    }

    public void addItem() {
        this.lineItems.add(this.getLineItem());
        this.itemTransaction.addItem(this.getLineItem());
        this.item = null;
        this.resetLineItem();
    }

    @Override
    public String processRequest() {
        this.baseService.purchaseItem((PurchaseDetails) this.itemTransaction);
        JsfUtil.addSuccessMessage("Save Sucessfull");
        return null;
    }

    public PurchaseDetails getPurchaseDetails() {
        return (PurchaseDetails) this.itemTransaction;
    }

    public SelectItem[] getVendors() {
        return JsfUtil.getSelectItems(baseService.findAllSupplier(), false);
    }

    @Override
    public void reset() {
        this.itemTransaction = new PurchaseDetails();
        this.item = new PurchaseLineItem();
    }

    @Override
    public void resetLineItem() {
        this.item = new PurchaseLineItem();
    }

    @Override
    public PurchaseLineItem getLineItem() {
        return (PurchaseLineItem) this.item;
    }
}
